package leetcode.offer;

import leetcode.base.ListNode;

/**
 * ClassName: DeleteNode
 * Description: leetcode-剑指offer 18.删除链表的节点
 * date: 2021-11-08 22:19
 *
 * @author liyifan
 */
public class DeleteNode {
    public ListNode deleteNode(ListNode head, int val) {
        if (head == null)
            return null;

        ListNode pre = null;
        ListNode cur = head;
        while (cur != null && cur.val != val) {
            pre = cur;
            cur = cur.next;
        }
        if (cur == head) {
            return cur.next;
        } else if (cur == null){
            return head;
        } else {
            pre.next = cur.next;
        }
        return head;
    }
}
